//
//  YDAlertButton.m
//  YDAlertShow
//
//  Created by James on 16/5/8.
//  Copyright © 2016年 bearbaby. All rights reserved.
//

#import "YDAlertButton.h"
#import "ViewController.h"
#import "YDAlertView.h"
@implementation YDAlertButton
-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        self.titleLabel.font = [UIFont systemFontOfSize:17];
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor whiteColor];
        [self addTarget:self action:@selector(buttonTarget) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return self;
}
-(void)buttonTarget{

    if (self.handle) {
        self.handle();
        UIResponder *nexResponder = self.nextResponder.nextResponder.nextResponder;
      __block    UIView *backView = (UIView *)nexResponder;
         __block YDAlertView *ydView = (YDAlertView *)self.nextResponder.nextResponder;
       
        
        [UIView animateWithDuration:0.2 animations:^{
            ydView.alpha = 0;
        } completion:^(BOOL finished) {
           // [backView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            
            [ydView.titleView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            [ydView.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            [ydView.buttonView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            
            [backView removeFromSuperview];
        }];

    }
    if (self.textHandle) {
        self.textHandle(self.handleText);
        UIResponder *nexResponder = self.nextResponder.nextResponder.nextResponder;
        __block    UIView *backView = (UIView *)nexResponder;
        __block YDAlertView *ydView = (YDAlertView *)self.nextResponder.nextResponder;
        
        
        [UIView animateWithDuration:0.2 animations:^{
            ydView.alpha = 0;
        } completion:^(BOOL finished) {
            // [backView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            
            [ydView.titleView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            [ydView.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            [ydView.buttonView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            
            [backView removeFromSuperview];
        }];

    }

}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [super touchesBegan:touches withEvent:event];
    self.backgroundColor = [UIColor colorWithHexString:@"ececec"];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [super touchesEnded:touches withEvent:event];
    self.backgroundColor = [UIColor whiteColor];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
